﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using SMS.QuanLyTinNhan;
using System.Data.SqlClient;

namespace SMS
{
    public partial class fLogin : Form
    {
        public uMenu ucMenu;
        public fCauHinhMayChu CauHinhMayChu;
        public fDoimatkhau DoiMatKhau;
        public fHethong HeThong;
        public fQuanlykhachhang QuanLyKhachHang;
        public FQuanlytaikhoan QuanLyTaiKhoan;
        public fQuanlytinnhan QuanLyTinNhan;
        public fQuanlyxe QuanLyXe;
        public fTraCuuSinhNhatKhachHang TraCuuSinhNhatKhachHang;
        public fThongTinKhachHang Thongtinkhachhang;
        public fCapNhatThongTinXe Capnhatthongtinxe;
        public fThemLoaiXe Themloaixe;
        public fThemMauXe Themmauxe;
        public fTonKho Tonkho;

        public uDanhsachkhachhang Danhsachkhachhang;
        public uImportthongtinkhachhang Importkhachhang;
        public uThemMoiKhachHang Themkhachhang;

        public uDanhsachxe Danhsachxe;
        public uImportXe Importxe;
        public uThemMoiXe Themmoixe;

        public uDanhsachtaikhoan Danhsachtaikhoan;
        public uThemtaikhoan Themtaikhoan;

        public uThietlapcauhinhtinnhan Cauhinhtinnhan;
        public uLichsutinnhan Lichsutinnhan;

        public SMS.QuanLyTinNhan.fThongBaoSoTinNhanCanGui ThongBaoTinNhanCanGui;
        public fThongtintaikhoan ThongTinTaiKhoan;

        public bool DB_BaoVe;
        public string DB_Username;
        public string DB_Pass;
        public string DB_ServerName;
        public string DB_DBName;
        public string ConnectDB;


        public SerialPort port = new SerialPort();
        public GuiSMS objclsSMS = new GuiSMS();
        public KetNoiDataBase KetNoiDB;
        public SMS.Object.MauXe Mauxe;
        public SMS.Object.TenXe Tenxe;
        public KhachHang khachhang;

        public TinNhan tinnhan;

        public bool checkDB ;
        



        public fLogin()
        {
            InitializeComponent();
            //checkDB = false;
            QuanLyKhachHang = new fQuanlykhachhang(this);
            HeThong = new fHethong(this);
            QuanLyTinNhan = new fQuanlytinnhan(this);
            QuanLyXe = new fQuanlyxe(this);
            DoiMatKhau = new fDoimatkhau(this);
            ucMenu = new uMenu(this);
            //CauHinhKetNoi = new fCauHinhKetNoi(this);
            CauHinhMayChu = new fCauHinhMayChu(this);
            QuanLyTaiKhoan = new FQuanlytaikhoan(this);
            TraCuuSinhNhatKhachHang = new fTraCuuSinhNhatKhachHang(this);
            Thongtinkhachhang = new fThongTinKhachHang(this);
            //Thongtintaikhoan = new fThongtintaikhoan();
            Capnhatthongtinxe = new fCapNhatThongTinXe(this);
            Themloaixe = new fThemLoaiXe(this);
            Themmauxe = new fThemMauXe(this);
            Tonkho = new fTonKho(this);

            Danhsachkhachhang = new uDanhsachkhachhang(this);
            Importkhachhang = new uImportthongtinkhachhang(this);
            Themkhachhang = new uThemMoiKhachHang(this);

            Danhsachxe = new uDanhsachxe(this);
            Importxe = new uImportXe(this);
            Themmoixe = new uThemMoiXe(this);

            Danhsachtaikhoan = new uDanhsachtaikhoan(this);
            Themtaikhoan = new uThemtaikhoan(this);

            Cauhinhtinnhan = new uThietlapcauhinhtinnhan(this);
            Lichsutinnhan = new uLichsutinnhan(this);
            ThongBaoTinNhanCanGui = new QuanLyTinNhan.fThongBaoSoTinNhanCanGui(this);
            ThongTinTaiKhoan = new fThongtintaikhoan(this);
            tinnhan = new TinNhan(this);
            KetNoiDB = new KetNoiDataBase(this);
            Mauxe = new Object.MauXe(this);
            Tenxe = new Object.TenXe(this);
            khachhang = new KhachHang(this);
        }


        private void lstCauHinhMayChu_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CauHinhMayChu.Visible = true;
            this.Visible = false;
        }
        private void bntDangNhap_Click(object sender, EventArgs e)
        {
            string Taikhoan = textBox1.Text;
            string Matkhau = textBox2.Text;
            SqlConnection conn;
            SqlCommand command;
            conn = new SqlConnection(ConnectDB);
            command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "select * from taikhoan where tentk=@tentk and matkhaumd=@mk";

            try
            {
                conn.Open();
                command.Parameters.Add(new SqlParameter("@tentk", textBox1.Text.ToString()));
                command.Parameters.Add(new SqlParameter("@mk", textBox2.Text.ToString()));
                SqlDataReader reader = command.ExecuteReader();

                if (Taikhoan == null || Taikhoan == "" || Matkhau == null || Matkhau == "")
                {
                    MessageBox.Show("Nhap tai khoan va mat khau");
                }

                else
                {

                    if (reader.Read())
                    {
                        this.Visible = false;
                        HeThong.Visible = true;
                    }
                    else
                    {
                        MessageBox.Show("Sai tai khoan hoac mat khau");
                    }
                }

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                conn.Close();
            }

        }
        private void bntThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fLogin_Load(object sender, EventArgs e)
        {
            while (true)
            {
                if (checkDB == false)
                {
                    CauHinhMayChu.ShowDialog();
                }
                else 
                {
                    break;
                }
                
            }
          
            if (CauHinhMayChu.chkIngSec.Checked == true)
            {
                ConnectDB = "Data source=" + DB_ServerName + ";Database=" + DB_DBName + ";User ID=" + DB_Username + ";Password=" + DB_Pass;
            }
            else
            {
                ConnectDB = "Data source=" + DB_ServerName + ";Database=" + DB_DBName + ";Integrated Security = true";
            }

            tinnhan.Lay_All();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (textBox1.Text == null || textBox1.Text == "" || textBox2.Text == null || textBox2.Text == "")
                {
                    MessageBox.Show("Bạn phải nhập đầy đủ các thông tin");
                }
                else
                {
                    bntDangNhap_Click(null, null);
                }
            }
        }
    }
}

